package cn.lamplet.util.kccu;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ParseSeg {

    private final Logger logger = LoggerFactory.getLogger(ParseSeg.class);

    ArrayList<Integer> segList;
    int segNumber;
    String dir;
    String segFileName;

    public ParseSeg(String dir, String segFileName) {
        this.dir = dir;
        this.segFileName = segFileName;
        segList = new ArrayList<>();
    }

    public ParseSeg() {
        segList = new ArrayList<>();
    }
    public void calSegs(ParseId parseId, Map<String, byte[]> fileToInputStream){
        segNumber=parseId.getHexFileList().size();
        Hex2bin hex2bin = new Hex2bin();
        for(String hexFile:parseId.getHexFileList()) {
            ArrayList<Integer> segAddrSize=hex2bin.handleHexSeg(new ByteArrayInputStream(fileToInputStream.get(hexFile)));
            segList.add(segAddrSize.get(0));
            segList.add(segAddrSize.get(1));
        }
    }
    public ParseSeg(InputStream inputStream) {
        this.segList = new ArrayList<>();
        try {
            List<String> lines = IOUtils.readLines(inputStream, StandardCharsets.UTF_8);
            for (String line : lines) {
                handleLine(line);
            }
        } catch (Exception e) {
            logger.error("解析seg文件异常", e);
        }
    }
    public void handleSeg() {
        File file = new File(dir, segFileName);
        String line;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            while ((line = reader.readLine()) != null) {
                handleLine(line);

            }
        } catch (Exception e) {
//            System.out.println(e.getStackTrace());
//        } finally {
//            try {
//                reader.close();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
            logger.error("解析seg文件异常", e);

        }
    }

    private void handleLine(String line) {
        if (line.charAt(0) == '#') return;
        String[] str = line.split(";");
        segNumber = Integer.parseInt(str[1], 10);
        for (int i = 2; i < str.length; i += 2) {
            if (str[i].equals("#")) {
                break;
            }
     //      System.out.println(str[i ].substring(2) + ":" + str[i + 1].substring(2));
/*            int i1=0;
//            System.out.printf("test seg %c %d\n",str[i].trim().charAt(2),(int)str[i].trim().charAt(2));
            if(str[i].trim().length()==10 &&str[i].trim().charAt(2)>'7'){
                System.out.println("seg length :"+str[i].trim().length()+" "+"str[i]="+str[i].trim().charAt(2));
                int hi1 = Integer.parseInt(str[i].trim().substring(2, 6), 16);
                int hi2 = Integer.parseInt(str[i].trim().substring(6, 10), 16);
                i1=(hi1<<16)|hi2;
                System.out.printf("seg1: %x %x %x\n",hi1,hi2,i1);

            }else {
                i1 = Integer.parseInt(str[i].trim().substring(2), 16);
                System.out.printf("seg2: %x\n",i1);

            }

 */
            int i1 = Integer.parseUnsignedInt(str[i].trim().substring(2), 16);
            segList.add(i1);
            int i2 = Integer.parseUnsignedInt(str[i + 1].trim().substring(2), 16);
            segList.add(i2);

//            System.out.printf("seg:   0x%x  0x%x\n", i1, i2);

        }

    }

    public ArrayList<Integer> getSegList() {

//        handleSeg();
//        for (int i = 0; i < segList.size(); i++) {
//            System.out.printf("%#x\n",segs.get(i));
//        }
        return segList;


    }
    public  int getSegNumber()
    {
        return  segNumber;
    }
}
